package com.yt.lxh.activity.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yt.lxh.activity.service.CouponInfoService;
import com.yt.lxh.common.result.ResponseResult;
import com.yt.lxh.common.result.ResultCodeEnum;
import com.yt.lxh.dto.activity.coupon.PublishCouponDTO;
import com.yt.lxh.dto.activity.coupon.QueryCouponDTO;
import com.yt.lxh.dto.activity.coupon.SaveCouponDTO;
import com.yt.lxh.dto.activity.coupon.UpdateCouponDTO;
import com.yt.lxh.enums.CouponStatus;
import com.yt.lxh.vo.activity.coupon.CouponPageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "优惠券管理接口")
@RestController
@RequestMapping("/admin/coupons")
@CrossOrigin
public class CouponController {

    @Autowired
    private CouponInfoService couponInfoService;

    @ApiOperation("获取优惠券分页数据")
    @PostMapping("/page")
    public ResponseResult getCouponPage(@RequestBody QueryCouponDTO query) {
        IPage<CouponPageVO> page = couponInfoService.getPage(query);
        return ResponseResult.ok(ResultCodeEnum.SUCCESS, page);
    }

    @ApiOperation("保存优惠券")
    @PostMapping("/save")
    public ResponseResult saveCoupon(@RequestBody @Validated SaveCouponDTO saveCouponDTO) {
        couponInfoService.saveCoupon(saveCouponDTO);
        return ResponseResult.ok(ResultCodeEnum.INSERT_SUCCESS);
    }

    @ApiOperation("发布优惠券")
    @PutMapping("/publish")
    public ResponseResult publishCoupon(@RequestBody @Validated PublishCouponDTO publishCouponDTO) {
        couponInfoService.publishCouponById(publishCouponDTO);
        return ResponseResult.ok(ResultCodeEnum.UPDATE_SUCCESS);
    }

    @ApiOperation("修改优惠券信息")
    @PutMapping("/update")
    public ResponseResult updateCoupon(@RequestBody @Validated UpdateCouponDTO updateCouponDTO) {
        couponInfoService.updateCoupon(updateCouponDTO);
        return ResponseResult.ok(ResultCodeEnum.UPDATE_SUCCESS);
    }

    @ApiOperation("修改优惠券状态")
    @PutMapping("/update/status/{id}/{status}")
    public ResponseResult updateStatusById(
            @PathVariable("id") String id,
            @PathVariable("status") CouponStatus couponStatus) {
        couponInfoService.updateCouponStatusById(id, couponStatus);
        return ResponseResult.ok(ResultCodeEnum.UPDATE_SUCCESS);
    }

    @ApiOperation("删除优惠券")
    @DeleteMapping("/del/{id}")
    public ResponseResult delCouponById(@PathVariable("id") String id) {
        couponInfoService.delCouponById(id);
        return ResponseResult.ok(ResultCodeEnum.DELETE_SUCCESS);
    }

}

